<template>
  <div class="wrapper p-4">
    <!-- 故障推送-->
    <template v-if="limits.includes('2_10_00_04')">
      <div class="d-flex justify-content-between align-items-center mb-2">
        <label class="font16" style="line-height: 32px">
          <i class="iconfont icon-biaotizhongdian txt_blue2" style="font-size: 20px;"></i>故障短信推送
        </label>
        <el-button v-if="limits.includes('2_10_00_03')" class="ur-btn ur-btn-primary w100" @click="saveSmsPush"><i class="iconfont icon-true"></i>保存</el-button>
      </div>
      <el-table class="checkItemTable mb-2" ref="checkItemTable" :data="tableData" tooltip-effect="dark" height="202">
        <el-table-column prop="checkItem" label="" align="center">
          <template slot-scope="scope">
            {{checkItems[scope.row.FaultNo]}}
          </template>
        </el-table-column>
        <el-table-column  label="重大报警"  align="center">
          <template slot-scope="scope">
            <el-radio v-model="scope.row.Level" :label="1">{{''}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="cell2" label="一般故障"  align="center">
          <template slot-scope="scope">
            <el-radio v-model="scope.row.Level" :label="2">{{''}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="cell3" label="一般事件" align="center">
          <template slot-scope="scope">
            <el-radio v-model="scope.row.Level" :label="3">{{''}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="cell4" label="短信推送"  align="center">
          <template slot-scope="scope">
            <el-checkbox  v-model="scope.row.SmsPush" ></el-checkbox>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
  import api from 'api/api'
  import {mapState} from 'vuex'
  export default {
    components: {},
    props: {
      boxs:{}
    },
    data() {
      return {
        checkItems:{20:'单灯电流过小',22:'单灯灯具故障'},
        tableData:[
          {FaultNo:20,FaultDes:'单灯电流过小',Level:1,SmsPush:true},
          {FaultNo:22,FaultDes:'单灯灯具故障',Level:1,SmsPush:true},
        ],
      }
    },
    watch: {},
    computed: {
      ... mapState({
        clientHeight: state => state.comm.clientHeight,
        clientWidth: state => state.comm.clientWidth,
        limits:state=>state.user.userInfo.limits
      })
    },
    methods: {
      saveSmsPush(){
        let items=[]
        this.tableData.map((item,index,arr)=>{
          let o = {...item}
          o.SmsPush = o.SmsPush ? 1 : 0
          for(let i=0;i<this.boxs.length;i++){
            items.push({BoxNo:this.boxs[i].No||this.boxs[i].BoxNo, ...o})
          }
        })
        this.saveSms(items,0)
      },
      saveSms(items,index){
        let currIndex=index
        api.fetch('/SmsPushConfig/Save',items[currIndex])
            .then(r=>{
              if(currIndex==items.length-1){
                this.$message.success({showClose:true,message:'设置成功'})
              }else{
                currIndex++
                this.saveSms(items,currIndex)
              }
            })
      },
      querySms(box){
        api.fetchSimple('/SmsPushConfig/QueryConfig',{pBoxNo:box.No||box.BoxNo})
            .then(r=>{
              let res=r.data
              res.map(item=>{
                item.SmsPush=item.SmsPush==1?true:false
              })
              this.tableData=res
            })
      }
    },
    created() {
      debugger
      if(this.boxs.length==1){
        this.querySms(this.boxs[0])
      }
    },
    mounted() {
    }
  }
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
</style>
